package instance;

import gameserver.instance.handlers.GeneralInstanceHandler;
import gameserver.instance.handlers.InstanceID;
import gameserver.model.drop.DropItem;
import gameserver.model.flyring.FlyRing;
import gameserver.model.gameobjects.Npc;
import gameserver.model.gameobjects.StaticDoor;
import gameserver.model.gameobjects.player.Player;
import gameserver.model.templates.flyring.FlyRingTemplate;
import gameserver.model.utils3d.Point3D;
import gameserver.network.aion.serverpackets.SM_QUEST_ACTION;
import gameserver.services.DropService;
import gameserver.utils.PacketSendUtility;
import gameserver.utils.ThreadPoolManager;
import gameserver.world.WorldMapInstance;

import java.util.Map;
import java.util.Set;

@InstanceID(300470000)
public class SatraTreasureHoardInstance extends GeneralInstanceHandler
{
	private Map<Integer, StaticDoor> doors;
	private boolean isStartTimer = false;
	private boolean isInstanceDestroyed;
	
	@Override
	public void onInstanceCreate(WorldMapInstance instance) 
	{
		super.onInstanceCreate(instance);
		doors = instance.getDoors();
		doors.get(77).setOpen(true);
		spawnTimerRing();
	}
	
	private void spawnTimerRing() 
	{
		FlyRing f1 = new FlyRing(new FlyRingTemplate("SATRAS_01", mapId,
			new Point3D(501.13412, 672.4659, 177.10771),
			new Point3D(492.13412, 672.4659, 177.10771),
			new Point3D(496.54834, 671.5966, 184.10771), 8), instanceId);
		f1.spawn();
	}
	
	@Override
	public boolean onPassFlyingRing(Player player, String flyingRing) 
	{
		if (flyingRing.equals("SATRAS_01")) 
		{
			if (!isStartTimer) {
				isStartTimer = true;
				System.currentTimeMillis();
				PacketSendUtility.sendPacket(player, new SM_QUEST_ACTION(0, 600));
				doors.get(77).setOpen(false);
				//Spawn Chest When the timer begin
				final Npc chest1 = (Npc) spawn(701461, 466.246f, 716.57f, 176.398f, (byte) 0);
				final Npc chest2 = (Npc) spawn(701461, 528.156f, 715.66f, 176.398f, (byte) 60);
				final Npc chest3 = (Npc) spawn(701461, 469.17f, 701.632f, 176.398f, (byte) 11);
				final Npc chest4 = (Npc) spawn(701461, 524.292f, 701.063f, 176.398f, (byte) 50);
				final Npc chest5 = (Npc) spawn(701461, 515.439f, 691.87f, 176.398f, (byte) 45);
				spawn(701461, 478.623f, 692.772f, 176.398f, (byte) 15);
				Npc muzzledPunisher = getNpc(219347);
				if (muzzledPunisher == null)
				{
					spawnElaborateChest();
				}
				else
				{
					spawnHeavyChest();
				}
				//Delete chest according with a timer (5 Min > 10) http://static.plaync.co.kr/powerbook/aion/23/83/3cbde3efd2422e9249efcfea.png
				ThreadPoolManager.getInstance().schedule(new Runnable() 
				{

					@Override
					public void run()
					{
						chest1.getController().delete();
					}

				}, 300000);
				ThreadPoolManager.getInstance().schedule(new Runnable() 
				{

					@Override
					public void run()
					{
						chest2.getController().delete();
					}

				}, 360000);
				ThreadPoolManager.getInstance().schedule(new Runnable() 
				{

					@Override
					public void run()
					{
						chest3.getController().delete();
					}

				}, 420000);
				ThreadPoolManager.getInstance().schedule(new Runnable() 
				{

					@Override
					public void run()
					{
						chest4.getController().delete();
					}

				}, 480000);
				ThreadPoolManager.getInstance().schedule(new Runnable() 
				{

					@Override
					public void run()
					{
						chest5.getController().delete();
					}

				}, 540000);
			}
		}
		return false;
	}
	
	private void spawnHeavyChest()
	{
		spawn(701462, 446.962f, 744.254f, 178.071f, (byte) 0, 206);
		spawn(701462, 459.856f, 759.960f, 178.071f, (byte) 0, 81);
		spawn(701462, 533.697f, 760.551f, 178.071f, (byte) 0, 80);
		spawn(701462, 477.382f, 770.049f, 178.071f, (byte) 0, 83);
		spawn(701462, 497.030f, 773.931f, 178.071f, (byte) 0, 85);
		spawn(701462, 516.508f, 770.646f, 178.071f, (byte) 0, 122);
	}

	private void spawnElaborateChest()
	{
		spawn(701463, 446.962f, 744.254f, 178.071f, (byte) 0, 206);
		spawn(701463, 459.856f, 759.960f, 178.071f, (byte) 0, 81);
		spawn(701463, 533.697f, 760.551f, 178.071f, (byte) 0, 80);
		spawn(701463, 477.382f, 770.049f, 178.071f, (byte) 0, 83);
		spawn(701463, 497.030f, 773.931f, 178.071f, (byte) 0, 85);
		spawn(701463, 516.508f, 770.646f, 178.071f, (byte) 0, 122);
	}
	
	@Override
	public void onDie(Npc npc) 
	{
		switch(npc.getNpcId()) 
		{
			case 219347: // Muzzled punisher
			case 219348: // Punisher unleashed
				spawn(730588, 496.600f, 685.600f, 176.400f, (byte) 30); // Spawn Exit
				break;
			case 701464: // Artifact spawn stronger boss
				Npc boss = getNpc(219347);
				if (boss != null && !boss.getLifeStats().isAlreadyDead())
				{
					spawn(219348, boss.getX(), boss.getY(), boss.getZ(), boss.getHeading());
					boss.getController().onDelete();
				}	
				break;
			case 219346:
				doors.get(118).setOpen(true);
				break;
			case 219345:
				doors.get(86).setOpen(true);
				break;
		}
	}
	
	@Override
	public void onDropRegistered(Npc npc)
	{
		Set<DropItem> dropItems = DropService.getInstance().getCurrentDropMap().get(npc.getObjectId());
		int npcId = npc.getNpcId();
		Integer object = instance.getSoloPlayerObj();
		switch (npcId)
		{
			case 219347:
				dropItems.add(DropService.getInstance().regDropItem(1, object, npcId, 185000118, 1));
				break;
			case 219348:
				dropItems.add(DropService.getInstance().regDropItem(1, object, npcId, 185000119, 1));
				break;
		}
	}
	
	private Npc getNpc(int npcId) {
        if (!isInstanceDestroyed) {
            return instance.getNpc(npcId);
        }
        return null;
    }
	
	@Override
	public void onInstanceDestroy() 
	{
		doors.clear();
	}
}